package com.stream.controller;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.stream.utils.R;
import com.stream.utils.ShiroUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 登录相关的接口
 * Created by yebaokang on 2017/5/24.
 */

@Controller
public class SysLoginController {

    @SuppressWarnings("all")
    @Autowired
    private Producer producer;

    @RequestMapping("captcha.jpg")
    public void kaptch(HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Cache-Control", "no-store,no-cache");
        response.setContentType("image/jpeg");

        //生成图形验证码
        String text = producer.createText();
        BufferedImage image = producer.createImage(text);
        //保存到 shiro Session
        ShiroUtils.setSessionAttribute(Constants.KAPTCHA_SESSION_KEY, text);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        out.flush();
    }

    /**
     * 登录
     * @param username
     * @param password
     * @param captcha
     * @return
     * @throws IOException
     */
    @ResponseBody
    @PostMapping("/sys/login")
//    @RequestMapping(value = "/sys/login", method = RequestMethod.POST)
    public R login(String username, String password, String captcha) throws IOException {
        String kaptcha = ShiroUtils.getKaptcha(Constants.KAPTCHA_SESSION_KEY);
        if (!(StringUtils.equalsIgnoreCase(captcha, kaptcha))) {
            return R.error("验证码不正确");
        }

        try {
            Subject subject = ShiroUtils.getSubject();
            //sha256加密
            password = new Sha256Hash(password).toHex();
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            subject.login(token);
        } catch (UnknownAccountException e) {
            return R.error(e.getMessage());
        } catch (IncorrectCredentialsException e) {
            return R.error(e.getMessage());
        } catch (LockedAccountException e) {
            return R.error(e.getMessage());
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return R.error("账户验证失败");
        }

        return R.ok();
    }

    /**
     * 退出
     *
     * @return
     */
//    @RequestMapping(value = "logout", method = RequestMethod.GET)
    @GetMapping("logout")
    public String logout() {
        ShiroUtils.logout();
//        重定向到登录页面
        return "redirect:login.html";
    }
}
